package com.sdy.supermarket.web.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdy.common.model.BizException;
import com.sdy.common.utils.Assert;
import com.sdy.common.utils.StringUtil;
import com.sdy.fs.api.FsApi;
import com.sdy.supermarket.biz.service.GsResFeatrueService;
import com.sdy.supermarket.biz.model.GsResFeatrue;
import com.sdy.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sdy.mvc.controller.BaseController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hyh
 * @since 2019-08-02
 */
@Slf4j
@RestController
@SuppressWarnings("unchecked")
@RequestMapping("/gsResFeatrue")
public class GsResFeatrueController extends BaseController {
    @Autowired
    private GsResFeatrueService gsResFeatrueService;
    @Autowired
    private FsApi fsApi;

    /**
     * 分页查询
     *
     * @param page pageSize,current
     * @param name 产品特色名称
     * @return
     */
    @GetMapping("/pageList")
    public Response pageList(Page<GsResFeatrue> page, String name) throws UnsupportedEncodingException {
        if (null != name) {
            name = URLDecoder.decode(name,"UTF-8");
        }
        IPage<GsResFeatrue> iPage = gsResFeatrueService.pageList(page, name);
        return Response.success(iPage);
    }

    /**
     * 根据ID获取数据
     *
     * @param id
     * @return
     */
    @GetMapping("/getById")
    public Response getById(Integer id) throws BizException {
        GsResFeatrue byId = gsResFeatrueService.getById(id);
        Assert.isNull(byId, "找不到该数据");
        if (StringUtil.isNotBlank(byId.getFeatureImgUrl()) && StringUtil.isNotEmpty(byId.getFeatureImgUrl())) {
            byId.setFileInfo(fsApi.getFileInfo(byId.getFeatureImgUrl()));
        } else {
            byId.setFileInfo(null);
        }
        return Response.success(byId);
    }

    /**
     * 根据ID删除数据(变更排序顺序)
     *
     * @param featrue
     * @return
     */
    @PostMapping("/deleteById")
    public Response deleteById(@RequestBody GsResFeatrue featrue) throws BizException {
        int delete = gsResFeatrueService.deleteById(featrue.getId());
        return delete != 0 ? Response.success("删除成功") : Response.error("删除失败");
    }


    /**
     * 排序提升or下降
     *
     * @param map id type
     * @return
     */
    @PostMapping("/sortUpOrDown")
    public Response sortUpOrDown(@RequestBody HashMap<String, Object> map) throws BizException {
        int i = gsResFeatrueService.sortUpOrDown((Integer) map.get("id"), (Integer) map.get("type"));
        return i != 0 ? Response.success("修改成功") : Response.error("修改失败");
    }

    /**
     * 新增&修改方法
     *
     * @param gsResFeatrue
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Response saveOrUpdate(@RequestBody GsResFeatrue gsResFeatrue) throws BizException {
        int i = gsResFeatrueService.saveOrUpdateRes(gsResFeatrue);
        return i != 0 ? Response.success("修改成功") : Response.error("修改失败");
    }


}
